﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Autodesk.AutoCAD.Interop;
using Autodesk.AutoCAD;
using Microsoft.VisualBasic;
using System.Windows.Forms;
using Autodesk.AutoCAD.Interop.Common;
using System.IO;
//using Autodesk.AutoCAD.Windows;
using MyIni;
using Autodesk.AutoCAD.GraphicsInterface;
namespace MyAutoCad
{
    public class MyAutoCad
    {
        Autodesk.AutoCAD.Interop.AcadApplication AcadApp;
        Autodesk.AutoCAD.Interop.AcadDocument AcadDoc;
        AcadSelectionSet mySelectionSet;
        //IniFile myini=new IniFile(Directory.GetCurrentDirectory()+"\\mycad.ini");
        int intset;

        /// <summary>
        /// 启动CAD文件
        /// </summary>
        public void StartCad()
        {
            try
            {
                AcadApp = (AcadApplication)System.Runtime.InteropServices.Marshal.GetActiveObject("AutoCAD.Application");
                AcadDoc = AcadApp.ActiveDocument;
            }
            catch
            {
                OpenFileDialog POpenDG = new OpenFileDialog();
                POpenDG.ShowDialog();
                string filePath = POpenDG.FileName;
                AcadApp = new AcadApplication();
                AcadDoc = AcadApp.Documents.Open(filePath, null, null);
            }
            AcadApp.Application.Visible = true;
            Microsoft.VisualBasic.Interaction.AppActivate(AcadApp.Caption);
        }
        public AcadObject[] GetSelSetAcdText()
        {
            //AcadDoc.SelectionSets
            return null;
        }

        //返回CAD选择集到数组
        public string[] GetSelectSet()
        {
            intset++;
            string NewSelectionSet003 = intset.ToString();
            Microsoft.VisualBasic.Interaction.AppActivate(AcadApp.Caption);
            // AcadSelectionSet mySelectionSet;
            mySelectionSet = AcadDoc.SelectionSets.Add(NewSelectionSet003);
            Int16[] FilterType = new Int16[1];
            object[] FilterData = new object[1];
            FilterType[0] = 0;
            FilterData[0] = "*";
            mySelectionSet.SelectOnScreen(FilterType, FilterData);
            int z = mySelectionSet.Count;
            String[] textArray=new String[z];
            int count = 0;
         
            foreach (AcadObject acadObj in mySelectionSet)
            {
                if (acadObj.ObjectName == "AcDbText" || acadObj.ObjectName == "AcDbMText")
                {
                    
                    textArray[count++]=((AcadText)acadObj).TextString;
                }
               
            }

            // textBox1.Text = showInforText;
            AcadDoc.SelectionSets.Item(NewSelectionSet003).Delete();
            Microsoft.VisualBasic.Interaction.AppActivate(AcadApp.Caption);
            return textArray;
        }
        public string[] GetSelectSet(string style)
        {
            intset++;
            string NewSelectionSet003 = intset.ToString();
            Microsoft.VisualBasic.Interaction.AppActivate(AcadApp.Caption);
            // AcadSelectionSet mySelectionSet;
            mySelectionSet = AcadDoc.SelectionSets.Add(NewSelectionSet003);
            Int16[] FilterType = new Int16[1];
            object[] FilterData = new object[1];
            FilterType[0] = 0;
            FilterData[0] = "*";
            mySelectionSet.SelectOnScreen(FilterType, FilterData);
            int z = mySelectionSet.Count;
            String[] textArray = new String[z];
            int count = 0;

            object p1 = new double[3];
            object p2 = new double[3];
            foreach (AcadObject acadObj in mySelectionSet)
            {
                //System.Diagnostics.Trace.TraceInformation(acadObj.GetType().ToString());
                if (acadObj.ObjectName == "AcDbText" || acadObj.ObjectName == "AcDbMText")
                {
                    textArray[count++] = ((AcadText)acadObj).TextString;
                    ((AcadText)acadObj).GetBoundingBox(out p1, out p2);
                    System.Diagnostics.Trace.TraceInformation(((double[])p1)[1] + " " + ((double[])p1)[0]);
                }
            }
            // textBox1.Text = showInforText;
            AcadDoc.SelectionSets.Item(NewSelectionSet003).Delete();
            Microsoft.VisualBasic.Interaction.AppActivate(AcadApp.Caption);
            return textArray;
        }
        //读选择集到mycad.ini的‘cad数据’节中
        //public void ReadSelectSetToIni()
        //{
        //    intset++;
        //    string NewSelectionSet003 = intset.ToString();
        //    Microsoft.VisualBasic.Interaction.AppActivate(AcadApp.Caption);
        //    mySelectionSet = AcadDoc.SelectionSets.Add(NewSelectionSet003);
        //    Int16[] FilterType = new Int16[1];
        //    object[] FilterData = new object[1];
        //    FilterType[0] = 0;
        //    FilterData[0] = "*";
        //    mySelectionSet.SelectOnScreen(FilterType, FilterData);

        //   // String[] textArray;
        //   // int count = 0;
        //   // string showInforText = "";
        //    int tempsum = myini.GetInt16("CAD数据", "sum", 0);
        //    foreach (AcadObject acadObj in mySelectionSet)
        //    {
        //        if (acadObj.ObjectName == "AcDbText" || acadObj.ObjectName == "AcDbMText")
        //        {
                   
        //            myini.WriteValue("CAD数据", (++tempsum).ToString(), ((AcadText)acadObj).TextString);
        //        }
        //        myini.WriteValue("CAD数据", "sum", tempsum);
        //    }

        //    AcadDoc.SelectionSets.Item(NewSelectionSet003).Delete();
        //    Microsoft.VisualBasic.Interaction.AppActivate(AcadApp.Caption);
        //}
        //读选择集到mycad.ini的指定section节中
        //public void ReadSelectSetToIni(string section)
        //{
        //    intset++;
        //    string NewSelectionSet003 = intset.ToString();
        //    Microsoft.VisualBasic.Interaction.AppActivate(AcadApp.Caption);
        //    mySelectionSet = AcadDoc.SelectionSets.Add(NewSelectionSet003);
        //    Int16[] FilterType = new Int16[1];
        //    object[] FilterData = new object[1];
        //    FilterType[0] = 0;
        //    FilterData[0] = "*";
        //    mySelectionSet.SelectOnScreen(FilterType, FilterData);

        //    // String[] textArray;
        //    // int count = 0;
        //    // string showInforText = "";
        //    int tempsum = myini.GetInt16(section, "sum", 0);
        //    foreach (AcadObject acadObj in mySelectionSet)
        //    {
        //        if (acadObj.ObjectName == "AcDbText" || acadObj.ObjectName == "AcDbMText")
        //        {

        //            myini.WriteValue(section, (++tempsum).ToString(), ((AcadText)acadObj).TextString);
        //        }
        //        myini.WriteValue(section, "sum", tempsum);
        //    }

        //    AcadDoc.SelectionSets.Item(NewSelectionSet003).Delete();
        //    Microsoft.VisualBasic.Interaction.AppActivate(AcadApp.Caption);
        //}
        public void WriteToCad(double[] p,String s,int height)
        {
            Microsoft.VisualBasic.Interaction.AppActivate(AcadApp.Caption);
            AcadDoc.ModelSpace.AddText(s, p, height);
        }
       
    }
}
